viby = {
    bubbles: [],

    loadScript: function (filename, onload) {
        var fileref = document.createElement('script');
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", filename);
        document.getElementsByTagName("head")[0].appendChild(fileref);
        if (onload) {
            fileref.onload = onload;
        }
    },

    addBubble: function (bubble) {
        var textbox = (bubble.direction === 'left') ? this.textleft : this.textright;
        textbox.append(bubble.render());
        bubble.onRender();
    },

    addFeedItem: function (bubble, data, choices) {
        if (!bubble) {
            bubble = new Bubble();
            this.bubbles[this.bubbles.length] = bubble;
            this.addBubble(bubble);
        }

        bubble.clearChoices();
        if (choices === 'yesno') {
            bubble.addChoice("Yes", 'button', function () {
                viby.userInput('yes');
            });
            bubble.addChoice("No", 'button', function () {
                viby.userInput('no');
            });
        } else if (choices === 'text') {
            var textChoice = bubble.addTextChoice('ok', function () {
                viby.userInput('weather ' + textChoice.getText());
            });
            textChoice.inputEl.focus();
        } else if (choices === 'back') {
            bubble.addChoice("Back", 'button', function () {
                viby.resetWeather();
            });
        } else if (choices === 'list') {
            var choiceFn = function (item) {
                viby.userInput('getillnessinfo ' + item);
            };
            for (listitem in data) {
                bubble.addChoice(data[listitem], 'link', choiceFn.bind(this, data[listitem]));
            }
            data = "You might have one of these health issues:<br/>";
            bubble.addChoice("Back", 'button', function () {
                viby.resetDoctor();
            });
        }

        bubble.setText(data);

        //this.feedbox.animate({ scrollTop: this.feedbox.prop("scrollHeight") }, 500);
        return bubble;
    },

    resetDoctor: function () {
        var doctor = this.doctorBubble;
        viby.setVibyStyle('mainpage_viby2.png', 'main_taustaga.jpg', '#D4EBD7');

        if (!doctor) {
            doctor = new Bubble();
            this.doctorBubble = doctor;
        }
        doctor.setText('<center><b>Doctor </b></center>');
        doctor.clearChoices();
        doctor.addChoice('Diagnose my symptoms', 'button', function () {
            viby.userInput('ill');
        });
        viby.addBubble(doctor);
    },

    resetWeather: function () {
        var weather = this.weatherBubble;
        viby.setVibyStyle('mainpage_viby2.png', 'main_taustaga.jpg', '#D4EBD7');

        if (!weather) {
            weather = new Bubble(true);
            this.weatherBubble = weather;
        }
        weather.setText('<center><b>Weather </b></center>');
        weather.clearChoices();
        weather.addChoice('Give me weather advice', 'button', function () {
            viby.addFeedItem(viby.weatherBubble, "What is your location?", 'text');
        });
        viby.addBubble(weather);
    },

    setVibyStyle: function (image, backgroundImage, backgroundColor) {
        $('#viby-area').css('background-image', 'url(images/' + image + ')');
        $('.page').css('background-image', 'url(images/' + backgroundImage + ')');
        $('body').css('background-color', backgroundColor);
    },

    userInput: function (text) {
        $.ajax({
            type: 'POST',
            url: "VibyService.asmx/Get",
            context: this,
            data: { usertext: text },
            success: function (response, a, b, c) {
                response = response.getElementsByTagName('string')[0];
                if (response.textContent) {
                    response = response.textContent;
                } else {
                    response = response.text;
                }
                response = JSON.parse(response);
                if (response.type == 'weather') {
                    viby.setVibyStyle('mainpage_viby3.png', 'main_taustaga.jpg', '#D4EBD7');
                    if (response.parameters[1] == 'cold') {
                        viby.setVibyStyle('ilm_volla2.png', 'main_taustagaKylm.jpg', '#e8ebfe');
                    } else if (response.parameters[1] == "hot") {
                        viby.setVibyStyle('ilm_volla.png', 'main_taustagaPalav.jpg', '#fae4b2');
                    }
                    this.addFeedItem(this.weatherBubble, response.parameters[0], 'back');
                }
                else if (response.type == 'asksymptom') {
                    this.addFeedItem(this.doctorBubble, response.parameters[0], 'yesno');
                }
                else if (response.type == 'illresults') {
                    viby.setVibyStyle('haigeViby.png', 'main_taustagaHaige.jpg', '#fad9ee');
                    this.addFeedItem(this.doctorBubble, response.parameters, 'list');
                }
                else if (response.type == 'illnessinfo') {
                    var text = "<b>Illness:</b> " + response.parameters[0] + '</br>' +
                        "<b>Symptoms:</b> " + response.parameters[1] + '</br>' +
                        "<b>Treatment:</b> " + response.parameters[2] + '</br>' +
                        "<b>Precaution:</b> " + response.parameters[3] + '</br>';

                    if (!this.illnessInfoBubble) {
                        bubble = new Bubble(false, true);
                        this.bubbles[this.bubbles.length] = bubble;
                        this.addBubble(bubble);
                        this.illnessInfoBubble = bubble;
                    }

                    this.addFeedItem(this.illnessInfoBubble, text);
                    this.illnessInfoBubble.el.width(300);
                    this.illnessInfoBubble.addChoice("Close", 'button', function () {
                        viby.illnessInfoBubble.el.remove();
                        delete viby.illnessInfoBubble;
                    });
                }
            },
            error: function () {
                this.addFeedItem('viby-text', "I couldn't connect to the server");
            }
        });

        //this.addFeedItem('user-text', text);
    }
};

$(document).ready(function () {
    viby.textleft = $('#text-area-left');
    viby.textright = $('#text-area-right');

    viby.loadScript('Scripts/Button.js', function () {
        viby.loadScript('Scripts/bubble.js', function () {
            viby.resetDoctor();
            viby.resetWeather();
        });
    });

    viby.loadScript('Scripts/eyecursor.js', function () {
        new vibyEye(100, 70, 60, 'viby-area');
        new vibyEye(130, 65, 60, 'viby-area');
    });

    viby.loadScript('Scripts/TextField.js');
    viby.loadScript('Scripts/HyperLink.js');
    /*
    var input = $('#input-box');
    input.focus();
    input.keypress(function (e) {
        if (e.which == 13 && input.val() !== '') {
            viby.userInput(input.val());
            input.val('');
            e.preventDefault();
        }
    });
    */
});
